package HLLib.userControl.HLPanelPickUp;

import javax.microedition.lcdui.Image;

import HLLib.base.HLGraphics;
import HLLib.base.HLGraphics_H;
import HLLib.base.HLImage;
import HLLib.base.HLString;
import HLLib.control.HLPanel;
import HLLib.control.HLPhotoFrame;
import HLLib.handfere.HLUIRes;
import HLLib.handfere.HLCommunityData.HLPoto.HLPhoto;

public class HLPhotoMiniPanel extends HLPanel {
    public static final int PHOTO_X = 3;

    public static final int PHOTO_Y = 5;

    public static final int PHOTO_TO_TEXT = 10;

    public static final int TEXT_Y = 2;

    public HLPhoto photo;

    HLPhotoFrame photoFrame;

    int xText;

    // HLTextPage textPage ;
    public int selectColor = 0xffff00;

    public int color = 0xffecf3fd;

    Image img;

    Image sex[] = HLUIRes.uires.GetSex();

    public HLPhotoMiniPanel(HLPhoto photo, int width, Image img) {
        this.photo = photo;
        this.width = (short) width;
        this.img = img;
        tabelStop = true;
        photoFrame = new HLPhotoFrame(photo.thumbImg);
        photoFrame.x = PHOTO_X;
        photoFrame.y = PHOTO_Y;

        Add(photoFrame);

        xText = PHOTO_X + photoFrame.width + PHOTO_TO_TEXT;
        this.height = Math.max(TEXT_Y + HLGraphics.StringHeight() + TEXT_Y + HLGraphics.StringHeight() + 2, photoFrame.height + PHOTO_Y * 2);
        needDownload = photo.thumbImg.GetImage1() == null;
        oriHeight = photo.thumbImg.GetHeight();
    }

    int oriHeight = 0;

    boolean haveDownloadOver = false;

    boolean needDownload = true;

    public void AdjustSize() {
        if (parent != null) {
            parent.AdjustSize();
        }
    }

    public void Logic() {

        if (needDownload) {
            if (photo.thumbImg != null && !haveDownloadOver) {
                if (photo.thumbImg.GetImage1() == null) {
                    haveDownloadOver = false;
                } else {
                    haveDownloadOver = true;
                    this.height += photo.thumbImg.GetHeight() - oriHeight;
                    AdjustSize();
                    GetUiRoot().Refresh();
                }
            }
        }

        super.Logic();
        if (IsFocus()) {
            RefreshEvent(GetScreenX(), GetScreenY(), GAME_BUTTON_ACTION);
        }
    }

    public void Refresh() {
        super.Refresh();

    }

    public void Render(HLGraphics g) {
        // super.Render(g);
        int screenX = GetScreenX();
        int screenY = GetScreenY();
        g.FillRect(screenX, screenY, width, height, 0xffffff);
        if (IsFocus()) {
            // g.DrawImageFillRect(new HLImage(img[1]), screenX, screenY, width,
            // img[1].getHeight(), HLGraphics_H.TRANS_NONE);
            // g.DrawImageFillRect(new HLImage(img[0]), screenX, screenY +
            // height - img[0].getHeight(), width, img[1].getHeight(),
            // HLGraphics_H.TRANS_NONE);
            g.FillRect(screenX, screenY, width, height, HLPanelGroup.SELECTED_COLOR);// d0d9f2);//0xd0ebf2);
            g.DrawImage(new HLImage(img), screenX + width - img.getWidth() - 5, screenY + (height - img.getHeight() >> 1), HLGraphics_H.TRANS_NONE);
        } else {

        }
        xText = PHOTO_X + photoFrame.width + PHOTO_TO_TEXT;
        int offsetX = screenX + xText;
        int offsetY = screenY + TEXT_Y;

        // g.DrawString(new HLString(photo.photoName), offsetX +
        // sex[0].getWidth() + 3, offsetY, 0xff0000ff, 0, TEXT_STYLE_NORMAL);
        int stringHeight = HLGraphics.StringHeight();

        offsetY += stringHeight + TEXT_Y;

        g.ClipRect(offsetX, screenY, width - offsetX - 15, height);
        g.DrawString(new HLString(photo.photoDes), offsetX, screenY + ((height - HLGraphics.StringHeight()) >> 1), 0xff4b4b4b, 0, TEXT_STYLE_NORMAL);
        g.SetClip(0, 0, HLLib.base.HLScreen.width, HLLib.base.HLScreen.height);

        photoFrame.Render(g);

    }

}
